package org.jeecg.modules.simulator.service;

import lombok.Setter;
import org.jeecg.modules.simulator.factory.train.plan.TimePlanFactory;
import org.jeecg.modules.simulator.jsonot.PlanTrainSchedular;
import org.jeecg.modules.simulator.mapper.PlanTrainSchedularMapper;
import org.jeecg.modules.simulator.mapper.PsgStationDistributionMapper;
import org.jeecg.modules.simulator.mapper.TrainPsgDistributionMapper;
import org.jeecg.modules.simulator.model.line.station.station.Station;
import org.jeecg.modules.simulator.model.line.station.station.TransferStation;
import org.jeecg.modules.simulator.model.train.TrainPhysics;
import org.jeecg.modules.simulator.model.train.timeplan.TrainPlanTable;
import org.jeecg.modules.simulator.util.TimeUtil;
import org.jeecg.modules.simulator.vo.VirtualTrainSchedularQueryCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class HistoryBizTrainLoaderService  {
    @Resource
    PlanTrainSchedularMapper planTrainSchedularMapper;

    @Setter
    public Map<String, Station> stationMap = new HashMap<>();

    @Setter
    Map<String, TransferStation> transferStationMap = new HashMap<>();


    VirtualTrainSchedularQueryCondition configure=new VirtualTrainSchedularQueryCondition();

    public void setVirtualTrainSchedularQueryCondition(VirtualTrainSchedularQueryCondition configure) {
        this.configure = configure;
    }


    Map<String, List<TrainPlanTable>> trainPlanTableMap = new HashMap<>();


    @Setter
    Boolean isAllDay;


    public void loadTimePlanData() {
        List<PlanTrainSchedular> planTrainSchedulars=new ArrayList<>();
        if(isAllDay) {
            planTrainSchedulars.addAll(planTrainSchedularMapper.getTrainSchedRecordsOrderAllDayByArrive(this.configure));
        } else {
            planTrainSchedulars.addAll(planTrainSchedularMapper.getTrainSchedRecordsOrderByArrive(this.configure));
        }


//        = planTrainSchedularMapper.getTrainSchedRecordsOrderAllDayByArrive(this.configure);
//        Map<String, List<PlanTrainSchedular>> myMap = planTrainSchedulars.stream()
//                .collect(Collectors.toMap(
//                        e -> e.getTrainId() + "_" + e.getTrainNum(), // 组合键
//                        e -> e, // 值就是对象本身
//                        (existing, replacement) -> existing // 处理重复键的情况，这里选择保留原有值
//                ));

        Map<String, List<PlanTrainSchedular>> groupedMap = planTrainSchedulars.stream()
                .collect(Collectors.groupingBy(e -> e.getTrainId() + "_" + e.getTrainNum()));
        List<TrainPlanTable> trainPlanTables = new ArrayList<>();
        groupedMap.forEach((key, value) -> {

            // 按 arriveTm 排序
            value.sort(Comparator.comparing(PlanTrainSchedular::getArriveTm));

            for (int i = 0; i < value.size(); i++) {
                TrainPlanTable trainPlanTable = new TrainPlanTable();
                PlanTrainSchedular planTrainSchedular = value.get(i);
                String arriveTime = TimeUtil.convertToHHmmss(planTrainSchedular.getArriveTm().toString());
                trainPlanTable.setArriveTime(arriveTime);
                trainPlanTable.setArrivateStaionId(planTrainSchedular.getStationId());
                if(stationMap.containsKey(planTrainSchedular.getStationId())) {

                    trainPlanTable.setArriveStaionName(stationMap.get(planTrainSchedular.getStationId()).getName());
                }else{

                    transferStationMap.forEach((k,v)->{
                        v.getTransferStationMap().forEach((k1,v1)->{
                            if(v1.getId().equals(planTrainSchedular.getStationId())){
                                trainPlanTable.setArriveStaionName(v1.getName());
                            }
                        });

                    });
                }
                String departTime = TimeUtil.convertToHHmmss(planTrainSchedular.getDeptrTm().toString());
                trainPlanTable.setDepartTime(departTime);
                int remains = TimeUtil.calculateSecondsDifference2(arriveTime, departTime);
                trainPlanTable.setRemainTime(remains);
                trainPlanTable.setTrainId(planTrainSchedular.getTrainId());
                trainPlanTable.setTrainNum(planTrainSchedular.getTrainNum());
                trainPlanTable.setIndex(i);
                trainPlanTable.setLineId(planTrainSchedular.getLineId());
                trainPlanTables.add(trainPlanTable);
            }


        });

        trainPlanTableMap = trainPlanTables.stream().collect(Collectors.groupingBy(TrainPlanTable::getLineId));
        System.out.println("打印测试");

    }






    public Map<String, List<TrainPlanTable>> getTrainPlanTableByLineMap() {
        return trainPlanTableMap;
    }
}
